local kuishi = fk.CreateSkill{
  name = "rfenghou_3d17j__kuishi",
}

---@param player ServerPlayer
local function getLastRoundStatus(player)
  local room = player.room
  local round_event = room.logic:getCurrentEvent():findParent(GameEvent.Round, true)
  local id = room.logic:getCurrentEvent().end_id
  if round_event then
    id = round_event.id
  end
  local round_events = room.logic:getEventsByRule(GameEvent.Round, 1, function (e)
    return e.id < id
  end, 0)
  if #round_events == 0 then return end
  local damaged = {}
  room.logic:getActualDamageEvents(1, function (e) ---@param e GameEvent.Damage
    if e.id < round_events[1].end_id and e.id > round_events[1].id then
      local damage = e.data
      if damage.from and damage.from ~= player then
        table.insertIfNeed(damaged, damage.from.id)
      end
    end
  end, nil, 0)
  room:setPlayerMark(player, "rfenghou_3d17j__kuishi_damaged", damaged)
end

kuishi:addEffect("viewas", {
  pattern = "foresight",
  mute_card = false,
  max_turn_use_time = 1,
  prompt = "#rfenghou_3d17j__kuishi",
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    if not table.find(player:getTableMark("rfenghou_3d17j__kuishi_damaged"), function(pid) return Fk:currentRoom():getPlayerById(pid):hasSkill("rfenghou_3d17j__jigong") end) then
      return
    end
    if player:getHandcardNum() ~= 1 then return end
    local card = Fk:cloneCard("foresight")
    card.skillName = kuishi.name
    card:addSubcards(player:getCardIds('h'))
    return card
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(kuishi.name, Player.HistoryTurn) == 0 and player:getHandcardNum() == 1 and table.find(Fk:currentRoom().alive_players, function(p) return p ~= player and p:hasSkill("rfenghou_3d17j__jigong") end)
  end,
  enabled_at_response = function(self, player)
    return player:usedSkillTimes(kuishi.name, Player.HistoryTurn) == 0 and player:getHandcardNum() == 1 and table.find(Fk:currentRoom().alive_players, function(p) return p ~= player and p:hasSkill("rfenghou_3d17j__jigong") end)
  end
})

kuishi:addAcquireEffect(function (self, player, is_start)
  if not is_start then
    getLastRoundStatus(player)
  end
end)

kuishi:addEffect(fk.RoundStart, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(kuishi.name)
  end,
  on_refresh = function(self, event, target, player, data)
    getLastRoundStatus(player)
  end,
})

kuishi:addEffect('filter', {
  card_filter = function(self, card, player, isJudgeEvent)
    if not (player:hasSkill(kuishi.name) and table.find(Fk:currentRoom().alive_players, function(p) return p ~= player and p:hasSkill("rfenghou_3d17j__jigong") end)) then return end
    if player:usedSkillTimes(kuishi.name, Player.HistoryTurn) ~= 0 then return end
    local handcard = player:getCardIds("h")
    return #handcard == 1 and handcard[1] == card.id and not table.find(player:getTableMark("rfenghou_3d17j__kuishi_damaged"), function(pid) return Fk:currentRoom():getPlayerById(pid):hasSkill("rfenghou_3d17j__jigong") end)
  end,
  view_as = function(self, player, card)
    local c = Fk:cloneCard("crafty_escape", card.suit, card.number)
    c.skillName = kuishi.name
    return c
  end
})

kuishi:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return data.card.name == "crafty_escape" and table.contains(data.card.skillNames, kuishi.name)
  end,
  on_refresh = function(self, event, target, player, data)
    player:addSkillUseHistory(kuishi.name, 1)
  end,
})

Fk:loadTranslationTable{
  ["rfenghou_3d17j__kuishi"] = "揆势",
  [":rfenghou_3d17j__kuishi"] = "每回合限一次，若拥有衍生技<a href=':rfenghou_3d17j__jigong'>〖急攻〗</a>的其他角色于上轮内未造成/造成过伤害，你的最后一张手牌 视为<a href=':crafty_escape'>【金蝉脱壳】</a>/可以当【洞烛先机】使用 。",

  ["#rfenghou_3d17j__kuishi"] = "揆势：将最后一张手牌当【洞烛先机】使用",
}

return kuishi
